package com.xhtt.modules.cfg.schedule;

import com.xhtt.common.utils.SpringContextUtils;
import com.xhtt.modules.cfg.entity.CfgPotInfo;
import com.xhtt.modules.cfg.entity.CfgPotLog;
import com.xhtt.modules.cfg.entity.CfgPotProduct;
import com.xhtt.modules.cfg.service.CfgPotInfoService;
import com.xhtt.modules.cfg.service.CfgPotLogService;
import com.xhtt.modules.cfg.service.CfgPotProductService;
import com.xhtt.modules.cfg.service.impl.CfgPotInfoServiceImpl;
import com.xhtt.modules.cfg.service.impl.CfgPotLogServiceImpl;
import com.xhtt.modules.cfg.service.impl.CfgPotProductServiceImpl;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Data
public class PotLog implements Runnable {
    private CfgPotInfoService potInfoService = SpringContextUtils.getBean(CfgPotInfoServiceImpl.class);
    private CfgPotProductService potProductService = SpringContextUtils.getBean(CfgPotProductServiceImpl.class);
    private CfgPotLogService potLogService = SpringContextUtils.getBean(CfgPotLogServiceImpl.class);

    private CfgPotInfo current;
    private CfgPotInfo old;
    private List<CfgPotProduct> oldProductList;
    private String deptUser;

    private PotLog() {
    }

    public PotLog(CfgPotInfo current, CfgPotInfo old, List<CfgPotProduct> oldProductList, String deptUser) {
        this.current = current;
        this.old = old;
        this.oldProductList = CollectionUtils.isEmpty(oldProductList) ? new ArrayList<>() : oldProductList;
        this.deptUser = deptUser;
    }

    @Override
    public void run() {
        try {
            String rfidInfo = "";
            String comProductInfo = "";
            String workAreaInfo = "";

            if (!ObjectUtils.nullSafeEquals(current.getRfid(), old.getRfid())) {
                rfidInfo = old.getRfid() + " → " + current.getRfid();
            }
            if (!ObjectUtils.nullSafeEquals(current.getWorkAreaName(), old.getWorkAreaName())) {
                workAreaInfo = old.getWorkAreaName() + " → " + current.getWorkAreaName();
            }

            CfgPotProduct curComProduct = null == current.getPotProduct() ? new CfgPotProduct() : current.getPotProduct();
            CfgPotProduct oldComProduct = oldProductList.stream().filter(p -> p.getType().intValue() == 1).findFirst().orElse(new CfgPotProduct());
            if (!ObjectUtils.nullSafeEquals(curComProduct.getProductId(), oldComProduct.getProductId())) {
                comProductInfo = (null == oldComProduct.getName() ? "" : oldComProduct.getName()) + " → " + (null == curComProduct.getName() ? "" : curComProduct.getName());
            }

            if (StringUtils.isNotEmpty(rfidInfo) || StringUtils.isNotEmpty(comProductInfo)|| StringUtils.isNotEmpty(workAreaInfo)) {
                CfgPotLog log = new CfgPotLog();
                log.setPotId(current.getId());
                log.setDeptUser(deptUser);
                log.setRfidInfo(rfidInfo);
                log.setWorkAreaInfo(workAreaInfo);
                log.setProductInfo(comProductInfo);
                potLogService.save(log);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
